package com.meeruu.sg.account.core.domain.service;

import com.meeruu.common.utils.LoggerUtil;
import com.meeruu.sg.account.core.api.exception.AccountException;
import com.meeruu.sg.account.core.api.exception.ErrorCode;
import com.meeruu.sg.account.core.domain.mapper.UserAccountMapper;
import com.meeruu.sg.account.core.domain.model.UserAccountDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author yangqintao
 * @date 2019-04-29
 */
@Slf4j
@Service
public class UserAccountService {

    @Resource
    private UserAccountMapper userAccountMapper;


    public UserAccountDO selectByAccountIdForUpdate(String accountId) {
        UserAccountDO userAccount = userAccountMapper.selectByAccountIdForUpdate(accountId);

        if (null == userAccount) {
            LoggerUtil.error(log, "账户不存在, accountId:{}", accountId);
            throw new AccountException(ErrorCode.BIZ_ACCOUNT_NOT_EXIST);
        }

        return userAccount;

    }

    /**
     * 更新账户金额
     *
     * @param availableBalance 可用余额更新
     * @param frozenBalance    冻结金额更新
     * @param totalBalance     总金额更新
     * @param accountId        账户id
     * @param beginBalance     更新前可用余额
     */
    public void updateAmount(BigDecimal availableBalance, BigDecimal frozenBalance, BigDecimal totalBalance,
                             String accountId, BigDecimal beginBalance) {

        userAccountMapper.updateAmount(availableBalance, frozenBalance, totalBalance, accountId, beginBalance);
    }
}
